// 加载和定义模块
var express = require('express'),
    bodyParser = require('body-parser'),
    cookieParser = require('cookie-parser'),
    session = require('express-session'),
    compression = require('compression'),
    ejs = require('ejs');

var port = process.env.Port || 3001;
var app = express();

app.use(compression());
app.use(bodyParser());
app.use(cookieParser());
app.use(session({ secret: '12345', cookie: { maxAge: 1000 * 60 * 240 } }));
app.set('views', __dirname + '/views'); // 配置模板目录
app.set('view engine', 'ejs');
app.use('/public', express.static(__dirname + '/public')); // 配置静态文件目录

app.all('*', function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
    res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');

    if (req.method == 'OPTIONS') {
        res.send(200);
    } else {
        next();
    }
});

// 路由配置
require("./routes/security")(app);
require("./routes/home")(app);
require("./routes/select")(app);
require("./routes/upgradeProject")(app);
require("./routes/upgradePackage")(app);
require("./routes/simpleUpgradePackage")(app);
require("./routes/simpleDownUpgradePackage")(app);
require("./routes/svnLog")(app);

// 启动定时器
//require("./app/timer/testNoticeTimer")();
//require("./app/timer/ugNoticeTimer")();
//require("./app/timer/svnLogNoticeTimer")();

app.listen(port, function() {
    console.log('upgradeCenter app started on port ' + port);
});